<!DOCTYPE html>
<html>
<head>
<title>sync.register() is rejected when the service worker is not active yet</title>
<script src="../resources/permissions-helper.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script>
</head>
<body>
<script>
async_test(function(test) {
    var workerUrl = '/resources/empty-worker.js';
    var workerScope = '/resources/scope/' + location.pathname;
    var swRegistration;
    PermissionsHelper.setPermission('background-sync', 'granted')
        .then(function() {
            return service_worker_unregister_and_register(test, workerUrl, workerScope);
        })
        .then(function(serviceWorkerRegistration) {
            swRegistration = serviceWorkerRegistration;
            assert_not_equals(swRegistration.installing, null, 'The worker should be installing');
            assert_equals(swRegistration.active, null, 'The worker should not be active yet');
            return swRegistration.sync.register('abcde');
        })
        .then(function(syncRegistration) {
            assert_unreached('sync.register() must not succeed without an active service worker');
        }, function(e) {
            assert_equals(e.name, 'InvalidStateError');
            assert_equals(e.message, 'Failed to execute \'register\' on \'SyncManager\': Registration failed - no active Service Worker');
            return service_worker_unregister_and_done(test, workerScope);
        })
        .catch(unreached_rejection(test));
}, 'sync.register() is rejected when the service worker is not active yet');
</script>
</body>
</html>
